﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System.IO;
using System.Collections;

namespace 一般三角网
{
    public partial class Form1 : Form
    {
        AcadApplication AcadApp;
        AcadDocument AcadDoc;
        private int ssetCount;

        public Form1()
        {
            InitializeComponent();
        }

        private void 启动CAD()
        {
            try
            {
                AcadApp =
               (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
                AcadDoc = AcadApp.ActiveDocument;
            }
            catch
            {
                OpenFileDialog pOpenDG = new OpenFileDialog();
                pOpenDG.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf";
                pOpenDG.Title = "打开CAD文件";
                pOpenDG.ShowDialog();
                string filePath = pOpenDG.FileName;
                if (filePath == "")
                {
                    MessageBox.Show("选择CAD文件无效！", "文件无效！");
                    Application.Exit();
                }
                AcadApp = new AcadApplication();
                AcadDoc = AcadApp.Documents.Open(filePath, null, null);
            }
            AcadApp.Application.Visible = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            启动CAD();
        }

        private void drawTrian(double[] anglePoints)
        {
            double[] p1 = new double[3], p2 = new double[3], p3 = new double[3];
            p1[0] = anglePoints[0]; p1[1] = anglePoints[1]; p1[2] = anglePoints[2];
            p2[0] = anglePoints[3]; p2[1] = anglePoints[4]; p2[2] = anglePoints[5];
            p3[0] = anglePoints[6]; p3[1] = anglePoints[7]; p3[2] = anglePoints[8];
            AcadDoc.ModelSpace.Add3DFace(p1, p2, p3, p1);
        }

        private void GetBaseTrian(double[] anglePoints)
        {
            ssetCount++;
            string selectionName = "NewSelectionSet" + ssetCount.ToString();
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add(selectionName);
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "POINT";
            mySelectionSet.SelectByPolygon(AcSelect.acSelectionSetWindowPolygon, anglePoints,
           FilterType, FilterData);
            if (mySelectionSet.Count > 0)
            {
                double[] insertPoint = (double[])((AcadPoint)mySelectionSet.Item(0)).Coordinates;
                AcadDoc.SelectionSets.Item(selectionName).Delete();
                double[] trian01 = new double[9];
                double[] trian02 = new double[9];
                double[] trian03 = new double[9];
                trian01[0] = anglePoints[0]; trian01[1] = anglePoints[1]; trian01[2] =
               anglePoints[2];
                trian01[3] = anglePoints[3]; trian01[4] = anglePoints[4]; trian01[5] =
               anglePoints[5];
                trian01[6] = insertPoint[0]; trian01[7] = insertPoint[1]; trian01[8] =
               insertPoint[2];
                trian02[0] = anglePoints[3]; trian02[1] = anglePoints[4]; trian02[2] =
               anglePoints[5];
                trian02[3] = anglePoints[6]; trian02[4] = anglePoints[7]; trian02[5] =
               anglePoints[8];
                trian02[6] = insertPoint[0]; trian02[7] = insertPoint[1]; trian02[8] =
               insertPoint[2];
                trian03[0] = anglePoints[6]; trian03[1] = anglePoints[7]; trian03[2] =
               anglePoints[8];
                trian03[3] = anglePoints[0]; trian03[4] = anglePoints[1]; trian03[5] =
               anglePoints[2];
                trian03[6] = insertPoint[0]; trian03[7] = insertPoint[1]; trian03[8] =
               insertPoint[2];

                GetBaseTrian(trian01);
                GetBaseTrian(trian02);
                GetBaseTrian(trian03);
            }
            else
            {
                drawTrian(anglePoints);
                AcadDoc.SelectionSets.Item(selectionName).Delete();
            }
        }

        private void 划分三角网_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取边界线!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            Acad3DPolyline borderLine = (Acad3DPolyline)returnObj;
            double[] borderPoints = (double[])borderLine.Coordinates;
            double[] anglepoints = new double[9];
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet01");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "POINT";

            mySelectionSet.SelectByPolygon(AcSelect.acSelectionSetWindowPolygon, borderPoints, FilterType,
            FilterData);
            double[] firstPoint = (double[])((AcadPoint)mySelectionSet.Item(0)).Coordinates;
            int i;
            for (i = 0; i < borderPoints.Length / 3 - 1; i++)
            {
                anglepoints[0] = borderPoints[3 * i]; anglepoints[1] = borderPoints[3 * i + 1];
                anglepoints[2] = borderPoints[3 * i + 2];
                anglepoints[3] = borderPoints[3 * i + 3]; anglepoints[4] = borderPoints[3 * i + 4];
                anglepoints[5] = borderPoints[3 * i + 5];
                anglepoints[6] = firstPoint[0]; anglepoints[7] = firstPoint[1]; anglepoints[8] =
               firstPoint[2];
                GetBaseTrian(anglepoints);
            }
            anglepoints[0] = borderPoints[3 * i - 3]; anglepoints[1] = borderPoints[3 * i - 2];
            anglepoints[2] = borderPoints[3 * i - 1];
            anglepoints[3] = borderPoints[0]; anglepoints[4] = borderPoints[1]; anglepoints[5] =
           borderPoints[2];
            anglepoints[6] = firstPoint[0]; anglepoints[7] = firstPoint[1]; anglepoints[8] =
           firstPoint[2];
            ssetCount = 0;
            GetBaseTrian(anglepoints);
            AcadDoc.SelectionSets.Item("NewSelectionSet01").Delete();
        }
    }
}
